﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAO;
using DTO;

namespace BUS
{
    public class DatMonBUS
    {
        public static int Addnew(DatMonDTO dm)
        {
            DataTable dt = DatMonDAO.Addnew(dm);
            if (dt.Rows.Count > 0)
                return int.Parse(dt.Rows[0]["ID"].ToString());
            return 0;

        }
        public static int GetMaDM(int ban)
        {
            DataTable dt = DatMonDAO.GetMaDM(ban);
            if (dt.Rows.Count>0)
            {
                return int.Parse(dt.Rows[0]["MADM"].ToString());
            }
            return 0;
        }
        public static string GetTopID()
        {
            string kq = string.Empty;
            DataTable dt = DatMonDAO.GetTopID();
            if (dt.Rows.Count > 0)
            {
                kq = dt.Rows[0]["MADM"].ToString();
            }
            return kq;
        }
        public static string NextID(string lastID, string prefixID)
        {
            if (lastID == string.Empty)
            {
                return prefixID + "001";
            }
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        public static bool Thanhtoan(int madm)
        {
            int kt = DatMonDAO.Thanhtoan(madm);
            if (kt > 0)
                return true;
            return false;
        }
    }
}
